<?php
namespace app\console\controller;

use app\api\controller\Aliyun;
use app\model\MemberBrokerage;
use app\model\MemberCert as SelfModel;
use app\model\Member;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Cookie;
class MemberCert extends Base
{



    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\MemberWithdraw";
        $this->list_rows = 20;
        
    }
    public function select()
    {
            $member = Member::where([['status', '=', 1]])->order(['sort' => 'asc', 'id' => 'desc'])->select()->toArray();
            View::assign('member', $member);
    }

    public function index()
    {

        $param = Request::param();


        $data = SelfModel::withSearch(array_keys($param), $param)->order(['id'=>'desc'])->paginate(['query' => $param, 'list_rows' => 20])
            ->each(function ($item, $key) {

        });

        $this->select();
        return View::fetch('', ['data' => $data]);
    }

    /*
     * 提现申请
     */
    public function update()
    {
        $param = \request()->param();
        $info = (new SelfModel())->find($param['id']);
        if ($this->request->isPost()) {
            if($param['audit_status'] == 2){
                $param['audit_time'] = date('Y-m-d H:i:s');
                if($param['type_id'] == 1){
                    //微信
                }
                if($param['type_id'] == 2){
                    //支付宝
                    $ret = (new Aliyun())->withdraw($param['order_number'],$param['brokerage'],$param['withdraw_no'],$param['withdraw_name']);//体现成功直接修改状态 失败退回

                    if($ret['code'] != 200){ //失败
                        $data['audit_status'] = 3;
                        $data['error_msg'] = $ret['msg'].' 账户已恢复操作前'.$param['before'];
                        //Member::where('id',$param['member_id'])->update(['brokerage' =>$param['before']]);//不可以多次提现 固定变量值写死 如果还继续分佣会写错
                        Member::where('id',$param['member_id'])->inc('brokerage',$param['brokerage'])->update();//不可以多次提现 固定变量值写死 如果还继续分佣会写错
                        $ret = $info->save($data);
                        if ($ret !== false) {
                            jump('提现失败', Route::buildUrl('index'));
                        }
                    }
                }

                $ret = $info->save($param);
                if ($ret !== false) {
                    win('提现成功', Route::buildUrl('index'));
                }
            }
            if($param['audit_status'] == 1){
                $ret = $info->save($param);
                if ($ret !== false) {
                    win('修改成功', Route::buildUrl('index'));
                }
                fail('失败');
            }

//            if(!empty($param['audit_status']) && $param['audit_status'] == 3){
//
//            }

        }
        $this->select();
        return View::fetch('create', ['info' => $info]);
    }

    public function audit_status()
    {
        Db::startTrans();
        try {
            $param = Request::param();
            $info = $this->model::where('id', $param['id'])->where('audit_status', 1)->find();

            if ($info['type_id'] == 2) {
                //支付宝
                $ret = (new Aliyun())->withdraw($info['order_number'], $info['brokerage'], $info['withdraw_no'], $info['withdraw_name']);//体现成功直接修改状态 失败退回

                if ($ret['code'] != 200) { //失败
                    if($ret['msg'] == 40004){
                        return json(['code' => $ret['code'], 'msg' => $ret['msg']]);//如果余额不足直接停止;不修改 同一个订单号转账OK了再次转账还是返回10000但是不继续转钱了
                    }
                    $data['audit_status'] = 3; //如果账号或者密码错误就得返还
                    $data['error_msg'] = $ret['msg'];
                    MemberBrokerage::saveData($info['member_id'],$info['member_id'],'佣金返还','+',$info['brokerage'],'','');
                    Db::commit();
                    $mw_ret = $info->save($data);
                    return json(['code' => 401, 'msg' => $ret['msg']]);//401审核失败刷新
                    //Member::where('id',$param['member_id'])->update(['brokerage' =>$param['before']]);//不可以多次提现 固定变量值写死 如果还继续分佣会写错
                }

            }
            //
            Db::commit();
            $ret = $this->model::where('id', $param['id'])->where('audit_status', 1)->update(['audit_status' => 2]);
            if($ret){
                return json(['code' => 200, 'msg' => '审核成功']);
            }
            return json(['code' => 400, 'msg' => '审核修改失败了']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 400, 'msg' => $e->getMessage()]);
        }
    }




}